<%@ page language="java" pageEncoding="UTF-8"%>

<script type="text/javascript">

var userId = "${data.id}";
var bindedRoleList;
$("#bindedRoleList").ready(function() {
	bindedRoleList = $("#bindedRoleList").jslist({
		core: { 
			listUrl:ctx+"/biz/system/role/bind/list.json",
			countUrl:ctx+"/biz/system/role/bind/count.json",
			queryParams: {"userId": userId},
			columnMetadatas:[{width:100,title:"名称",name:"name"},
							{width:100,title:"编码",name:"code"},
							{title:"描述",name:"content"}]},
		selectable: {selectType:"checkbox"},
		plugins:["selectable"]
	})
	.bind("list_count_retrieved.jslist",function(event,data) {
		$("#bindedRoleListSummary").html("("+data.retrievedCount+"/"+data.totalCount+"条)"); 
	})
	.jslist("refresh");
});

function bindRole() {
	$.openDialog("#dialog",
			{loadPage:ctx+"/biz/system/role/unbind/query.jsp?userId="+userId,
			title:"选择角色"},
			function(data) {
				bindedRoleList.jslist("refresh");
			});
}

function unbindRole() {
	var selectedItems = bindedRoleList.jslist("getSelectedItems");//$("#queryResultBody").find(":checkbox:checked");
	if (selectedItems.length == 0) {
		return;
	}
	
	if (!confirm('是否取消绑定？\n请确认是否继续操作 ！')) {
		return;
	}
	
	var roleIds = $(selectedItems).map(function(i,item){
		return item.id;
	}).get().join(",");

	doUnbindRole(userId,roleIds);
}

function doUnbindRole(userId,roleIds) {
	block("#bindRolePanel");
	$.ajax({
		type: "POST",
		url: "unbindRole.json",
		data: {
			"userId":userId,
			"roleIds":roleIds
		},
		dataType:"json",
		success: function(data){
			unblock("#bindRolePanel");
			$.alert("已取消绑定");
			bindedRoleList.jslist("refresh");
		},
		error:function(message){
			unblock("#bindRolePanel");
			$.alert(e);
		}
	});
}

function refreshRoles() {
	bindedRoleList.jslist("refresh");
}

</script>

<div id="bindedRoleList">
</div>

<div class="form-actions">
	<a href="javascript:;" class="btn btn-primary" onclick="bindRole()"><i class="icon-ok"></i>绑定角色</a>
	<a href="javascript:;" class="btn btn-warning" onclick="unbindRole()"><i class="icon-ok"></i>取消绑定</a>
	<a href="javascript:;" class="btn" onclick="refreshRoles()"><i class="icon-ok"></i>刷新</a>
</div>
